using System;
using Dto;
using UnityEngine;

namespace Character
{
    public class CardCtrl : MonoBehaviour
    {
        public CardDto CardDto { get; private set; }
        public bool Selected { get; set; }
        private SpriteRenderer _spriteRenderer;
        private bool isMine;
        /**
         * 初始化卡牌数据
         */
        public void Init(CardDto card, int index, bool isMine)
        {
            _spriteRenderer = GetComponent<SpriteRenderer>();
            this.CardDto = card;
            this.isMine = isMine;
            if (Selected)
            {
                Selected = false;
                transform.localPosition -= new Vector3(0, 0.3f, 0);
            }

            string resPath = string.Empty;
            // 自己的牌
            if (isMine)
            {
                resPath = "Poker/" + card.name;
            }
            else
            {
                resPath = "Poker/CardBack";
            }

            Sprite sp = Resources.Load<Sprite>(resPath);
            _spriteRenderer.sprite = sp;
            _spriteRenderer.sortingOrder = index;
        }

        private void OnMouseDown()
        {
            if (isMine == false)
            {
                return;
            }

            this.Selected = !Selected;
            if (Selected)
            {
                transform.localPosition += new Vector3(0, 0.3f, 0);

            }
            else
            {
                transform.localPosition -= new Vector3(0, 0.3f, 0);
            }
        }
    }
}